package br.com.advance.automacao.dao.impl;

import java.io.Serializable;
import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import br.com.advance.automacao.dao.CLPDao;
import br.com.advance.automacao.generic.dao.impl.GenericDaoImpl;
import br.com.advance.automacao.model.CLP;

@Repository("clpDao")
public class CLPDaoImpl extends GenericDaoImpl<CLP, Serializable> implements CLPDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<CLP> buscarTodosAtivos() {
		Query query = entityManager.createNamedQuery(CLP.BUSCAR_TODOS_ATIVOS);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CLP> buscarTodosAtivosComErrosPossiveis() {
		Query query = entityManager.createNamedQuery(CLP.BUSCAR_TODOS_ATIVOS_COM_ERROS);
		return query.getResultList();
	}

	@Override
	public CLP buscarPorNome(String nome) {
		Query query = entityManager.createNamedQuery(CLP.BUSCAR_POR_NOME);
		query.setParameter("nome", nome);
		try {
			return (CLP) query.getSingleResult();
		} catch(NoResultException e) {
			return null;
		}
	}

	@Override
	public CLP buscarPorIp(String ip) {
		Query query = entityManager.createNamedQuery(CLP.BUSCAR_POR_IP);
		query.setParameter("ip", ip);
		try{
			return (CLP) query.getSingleResult();
		} catch(NoResultException e) {
			return null;
		}
	}

	@Override
	public CLP buscarPorIdComErros(Long id) {
		Query query = entityManager.createNamedQuery(CLP.BUSCAR_POR_ID_COM_ERROS);
		query.setParameter("id", id);
		try{
			return (CLP) query.getSingleResult();
		} catch(NoResultException e) {
			return null;
		}
	}

}
